home *** CD-ROM | disk | FTP | other *** search
/ Gigantic Games 2 / Gigantic Games 2.iso / pc / _w_ / wordsearch / src / puzzle.c < prev    next >
C/C++ Source or Header  |  1994-12-23  |  3KB  |  154 lines

  1. #include <stdio.h>
  2. #include <exec/types.h>
  3. #include <exec/libraries.h>
  4. #include "interface.h"
  5. #include <libraries/reqbase.h>
  6. #include <proto/req.h>
  7. #include "wsearch.h"
  8. #include "funcs.h"
  9.  
  10. extern BOOL DispKey;
  11.  
  12. BOOL AllocDims()
  13. {
  14.    char *tkey, *tpuzzle, *tdisplay;
  15.    
  16.    tkey = key;
  17.    tpuzzle = puzzle;
  18.    tdisplay = display;
  19.  
  20.    key = (char *)calloc((px+1)*(py+1),sizeof(char));
  21.    if(key<=0)
  22.    {
  23.     SimpleRequest("Unable to Allocate Memory for new Key");
  24.     key = tkey;
  25.     return(FALSE);
  26.    }
  27.    puzzle = (char *)calloc((px+1)*(py+1),sizeof(char));
  28.    if(puzzle<=0)
  29.    {
  30.     SimpleRequest("Unable to Allocate Memory for new Puzzle");
  31.     puzzle = tpuzzle;
  32.     free(key);
  33.     key = tkey;
  34.     return(FALSE);
  35.    }
  36.    display = (char *)calloc((max(px,py)+1)*(max(px,py)+2),sizeof(char));
  37.    if(display<=0)
  38.    {
  39.     SimpleRequest("Unable to Allocate Memory for new Display");
  40.     display = tdisplay;
  41.     free(key);
  42.     key = tkey;
  43.     free(puzzle);
  44.     puzzle = tpuzzle;
  45.     return(FALSE);
  46.    }
  47.        
  48.    if(tkey!=NULL)
  49.     free(tkey);
  50.    if(tpuzzle!=NULL)
  51.     free(tpuzzle);
  52.    if(tdisplay!=NULL)
  53.     free(tdisplay);
  54.    return(TRUE);
  55. }
  56.  
  57. BOOL Dimensions()
  58. {
  59.    struct GetLongStruct GLS;
  60.    
  61.    GLS.minlimit = 2;
  62.    GLS.maxlimit = MAXPUZ;
  63.    GLS.versionnumber = REQVERSION;
  64.    GLS.flags = 0;
  65.    GLS.rfu2 = 0;
  66.    GLS.window = DPWin;
  67.    
  68.    GLS.titlebar = "Enter Width of Puzzle";
  69.    GLS.defaultval = px+1;
  70.    if(GetLong(&GLS)==FALSE) return(FALSE);
  71.    px = GLS.result-1;
  72.    GLS.titlebar = "Enter Height of Puzzle";
  73.    GLS.defaultval = py+1;
  74.    if(GetLong(&GLS)==FALSE) return(FALSE);
  75.    py = GLS.result-1;
  76. /*   printf("Enter puzzle dimensions (width height)\n");
  77.    scanf("%d %d",&px,&py);
  78.    px = px-1; py=py-1;
  79.  */
  80.    if(AllocDims()==FALSE)
  81.        return(FALSE);
  82.    else
  83.        return(TRUE);
  84. }
  85.  
  86. void NewKey()
  87. {
  88.     int i,j,error;
  89.  
  90.     for(i=0;i<=px;i++)
  91.         for(j=0;j<=py;j++)
  92.             Key(i,j) = ' ';
  93.  
  94.      error = wsearch();
  95.      if(error!=0 && error!=-1)
  96.      {
  97.         SimpleRequest("Cannot generate Key (a word won't fit)");
  98.      }
  99. }
  100.  
  101. void NewPuzzle()
  102. {
  103.      int i,j;
  104.  
  105.      for(i=0;i<=px;i++)
  106.         for(j=0;j<=py;j++)
  107.             if(Key(i,j) == ' ')
  108.                 Puzzle(i,j) = (char)(randint(25)+65);
  109.             else
  110.                 Puzzle(i,j) = Key(i,j);
  111. }
  112.  
  113. void NewDisplay()
  114. {
  115.       int xs,xt,xi,ys,yt,yi;
  116.       int i,j,k,l;
  117.       char *ptr;
  118.       
  119.       if(DispKey==TRUE)
  120.           ptr = &Key(0,0);
  121.       else
  122.           ptr = &Puzzle(0,0);
  123.  
  124.       switch(rot)
  125.        {
  126.          case 1:xs=0 ;ys=0 ;xt=px+1;yt=py+1;xi= 1;yi= 1;break;
  127.          case 2:xs=px;ys=0 ;xt=-1  ;yt=py+1;xi=-1;yi= 1;break;
  128.          case 3:xs=0 ;ys=py;xt=px+1;yt=-1  ;xi= 1;yi=-1;break;
  129.          case 4:xs=px;ys=py;xt=-1  ;yt=-1  ;xi=-1;yi=-1;break;
  130.          case 5:xs=0 ;ys=0 ;xt=py+1;yt=px+1;xi= 1;yi= 1;break;
  131.          case 6:xs=0 ;ys=px;xt=py+1;yt=-1  ;xi= 1;yi=-1;break;
  132.          case 7:xs=py;ys=0 ;xt=-1  ;yt=px+1;xi=-1;yi= 1;break;
  133.          case 8:xs=py;ys=px;xt=-1  ;yt=-1  ;xi=-1;yi=-1;break;
  134.        }
  135.  
  136.       l = 0;
  137.  
  138.       for(j=ys;j!=yt;j=j+yi)
  139.        {
  140.          k = 0;
  141.          for(i=xs;i!=xt;i=i+xi)
  142.          {
  143.           if(rot<5)
  144.                Display(l,k) = *(ptr + i*(py+1) + j);
  145.           else
  146.                Display(l,k) = *(ptr + j*(py+1) + i);
  147.           k++;
  148.          }
  149.          Display(l,k)=0;
  150.          l++;
  151.        }
  152. }
  153.  
  154.